<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * 会员控制器
 */
class UserController extends AdminController{
    public $pagesize=10;

    public function addUser(){

    }

    public function userList(){
        $count = M('users')->count();
        $page = new \Think\Page($count,$this->pagesize);
        $page ->setConfig('theme',"%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%");
        $show = $page->show(); //分页显示输出
        $list = M('users')->limit($page->firstRow.','.$page->listRows)->select();

        $this->assign('_page',$show);
        $this->assign('_list',$list);
        $this->display('user_list');
    }

    public function editUser(){

    }

    public function addRank(){
        if(IS_POST){
            $data = array(
                    'rank_name' => I('post.rankname','','trim'),
                    'min_points' => I('post.minpoint','','trim'),
                    'max_points' => I('post.maxpoint','','trim'),
                    'discount' => I('post.discount','','trim'),
                    'show_price' => I('post.isshow','','intval'),
                    'special_rank' => I('post.isspecial','','intval'),
                );
            //todo validate
            $isexists=M('user_rank')->where(array('rank_name'=>$data['rank_name']))->find();
            if($isexists){
                $this->error('该会员等级存在');
            }
            
            $add=M('user_rank')->data($data)->add();
            if($add){
               $this->success('添加成功',U('User/userRank'));     
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->display('add_rank');
        }
    }


    public function userRank(){
        $count = M('user_rank')->count();
        $page = new \Think\Page($count,$this->pagesize);
        $page ->setConfig('theme',"%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%");
        $show = $page->show(); //分页显示输出
        $list = M('user_rank')->limit($page->firstRow.','.$page->listRows)->select();

        $this->assign('_page',$show);
        $this->assign('_list',$list);
        $this->display('user_rank');
    }

    public function editRank(){

    }

    /**
     * TODO
     * 删除要关联其他表
     * @return [type] [description]
     */
    public function delRank(){

    }
}